package com.ruoyi.stock2.thread.aboutDownloadData;

import com.ruoyi.stock2.domain.TRADEDATA;
import com.ruoyi.stock2.utils.DateUtils2;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CheckSourceReadyFromQQ1 {
	

	public static String checkSourceReady() {
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		String year = String.valueOf((Integer.valueOf(sFormat.format(date).substring(0,4))-2000));
		String url2 = "http://data.gtimg.cn/flashdata/hushen/daily/"+year+"/sz399001.js";
		//String url2 = "http://data.gtimg.cn/flashdata/hushen/daily/"+year+"/sz000001.js";
		BufferedReader bufferedReader = null;
		try {
			Response resultImageResponse = Jsoup.connect(url2)
					.ignoreContentType(true)
					.execute();
			bufferedReader = new BufferedReader(new InputStreamReader(resultImageResponse.bodyStream(), "GBK"));
			String s = bufferedReader.readLine();

			String [] lastTradeLine = null;
			while ((s = bufferedReader.readLine()) != null ) {
				String [] lines = s.split("\\s");
				if(lines.length==6){
					String tradeDate = "20"+lines[0];
					//if(tradeDate.compareTo("20201016")>0 && tradeDate.compareTo("20201029")<=0){
						String open = lines[1];
						String close= lines[2];
						String high = lines[3];
						String low = lines[4];
						String volume = lines[5].split("\\\\")[0];
						//Double amount = Double.parseDouble(volume) * Double.parseDouble(close);
					    BigDecimal amount = new BigDecimal(volume).multiply(new BigDecimal(close)).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP);
//						System.out.print("tradeDate "+tradeDate + " ");
//						System.out.print("open "+open + " ");
//						System.out.print("close "+close + " ");
//						System.out.print("high "+high + " ");
//						System.out.print("low "+low + " ");
//						System.out.print("volume "+volume + " ");
//						System.out.println("amount "+amount);
					//}

					lastTradeLine = lines;
				}

			}

			String tradeDate = "20"+lastTradeLine[0];
			String nowdate = sFormat.format(date);
			if(tradeDate.compareTo(nowdate)<=0){
				return tradeDate.substring(0,4)+"-"+tradeDate.substring(4,6)+"-"+tradeDate.substring(6,8);
			}

		}catch (java.net.SocketTimeoutException e1) {
			e1.printStackTrace();
		}catch (java.net.UnknownHostException e1) {
			e1.printStackTrace();
		}catch (org.jsoup.HttpStatusException e1) {
			//e1.printStackTrace();
		}catch (Exception e1) {
			e1.printStackTrace();
		}
			
		return null;
	} 

	public static void main(String[] args) {


		System.out.println(checkSourceReady());

		/*
		//https://finance.yahoo.com/quote/002739.SZ/history?p=002739.SZ
		String url2 = "http://data.gtimg.cn/flashdata/hushen/daily/20/sz000004.js";
		//http://data.gtimg.cn/flashdata/hushen/daily/20/sh000001.js
		//http://data.gtimg.cn/flashdata/hushen/daily/20/sz399001.js
		//http://data.gtimg.cn/flashdata/hushen/daily/20/sz399006.js
		BufferedReader bufferedReader = null;
		try {
			Response resultImageResponse = Jsoup.connect(url2)
					.ignoreContentType(true)
					.execute();
			bufferedReader = new BufferedReader(new InputStreamReader(resultImageResponse.bodyStream(), "GBK"));
			String s =bufferedReader.readLine();
			while ((s = bufferedReader.readLine()) != null) {
				System.out.println(s);
			}
		}catch (java.net.SocketTimeoutException e1) {
			e1.printStackTrace();
		}catch (java.net.UnknownHostException e1) {
			e1.printStackTrace();
		}catch (Exception e1) {
			e1.printStackTrace();
		}

		 */

	}
}
